import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'Login',
    component: () => import(/* webpackChunkName: "group-foo" */'../views/login.vue')
  },
  {
    path: '/menu',
    name: 'Menu',
    component: () => import(/* webpackChunkName: "group-foo" */'../views/menu.vue'),
    children: [
      {
        path: '/',
        name: "Home",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/home.vue')
      },
      {
        path: '/users',
        name: "Users",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/users.vue')
      },
      {
        path: '/rights',
        name: 'Rights',
        component: () => import(/* webpackChunkName: "group-foo" */'../views/rights.vue')
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () => import(/* webpackChunkName: "group-foo" */'../views/roles.vue')
      },
      {
        path: '/goods',
        name: 'Goods',
        component: () => import(/* webpackChunkName: "group-foo" */'../views/goods.vue')
      },
      {
        path: '/goods/add',
        name: 'Add',
        component: () => import(/* webpackChunkName: "group-foo" */'../components/goods_add.vue'),
        meta: { parentPath: '/goods' }
      },
      {
        path: '/orders',
        name: "Orders",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/orders.vue')
      },
      {
        path: '/categories',
        name: "Categories",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/categories.vue')
      },
      {
        path: '/params',
        name: "Params",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/params.vue')
      },
      {
        path: '/reports',
        name: "Reports",
        component: () => import(/* webpackChunkName: "group-foo" */'../views/reports.vue')
      },
    ]
  },

]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 登录权鉴
router.beforeEach((to, from, next) => {
  // 获取到登录时的token
  let token = localStorage.getItem('token')
  // 判断页面是否在登录页
  if (to.path !== "/") {
    // 判断token是否存在
    if (!token) {
      return next('/')
    }
    // 判断token是否过期 利用 JWTDecode 可以获取token过期时间
    // let { exp, name } = jwtDecode(token);
    // // 设置一个变量为现在的时间
    // let nowtime = new Date().getTime()
    // // 因为获取到的 exp为秒，所有将获取到的毫秒转成秒
    // nowtime = Math.floor(nowtime / 1000)
    // // 判断 token的过期时间减去现在的时间，如果大于0，next放行，小于等于0则跳转至登录页
    // if (exp - nowtime >= 0) {
    //   next()
    // } else {
    //   next('/')
    // }
  }
  next()
})


export default router
